package kr.hkit.androidcomplete03.ch22;

import java.util.ArrayList;

import kr.hkit.androidcomplete03.R;
import android.R.color;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class SurfView extends SurfaceView implements SurfaceHolder.Callback {

	Bitmap backImg;
	ArrayList<Ball> ballLists ;
	final static int DELAY = 50;
	
	SurfaceHolder mHolder;
	DrawThread mThread;
	
	public SurfView(Context context) {
		super(context);
		backImg = BitmapFactory.decodeResource(context.getResources(), R.drawable.mingyeong);
		mHolder = getHolder();
		mHolder.addCallback(this);
		ballLists = new ArrayList<Ball>();
		// TODO Auto-generated constructor stub
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_DOWN){
			synchronized (mHolder) {
				Ball newBall = Ball.create((int)event.getX(), (int)event.getY());
				ballLists.add(newBall);
			}
			return true;
		}
		return false;
	}
	
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		mThread.sizeChange(width, height);
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		mThread = new DrawThread(mHolder);
		mThread.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		mThread.bExit = true;
		for(;;){
			try {
				mThread.join();
				break;
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}


class DrawThread extends Thread{
	boolean bExit;
	int mWidth;
	int mHeight;
	SurfaceHolder mHolder;
	
	public DrawThread(SurfaceHolder mHolder) {
		super();
		this.mHolder = mHolder;
	}
	
	public void sizeChange(int width, int height){
		mWidth = width;
		mHeight = height;
	}
	@Override
	public void run() {
		Canvas canvas;
		Ball b;
		
		while(!bExit){
			//애니매이션 진행
			for(int i =0; i<ballLists.size(); i++){
				b = ballLists.get(i);
				b.Move(mWidth, mHeight);
				if(b.count > 4){
					ballLists.remove(i);
					i--;
				}// end of if
			}//end of for
			
			synchronized (mHolder) {
				canvas = mHolder.lockCanvas();
				//배경 화면 설정
				Rect dst = new Rect(0, 0, getWidth(), getHeight());
				canvas.drawBitmap(backImg, null, dst, null);
				// 그리기
				if(canvas == null){
					break;
				}
				
				
				for(int i=0; i<ballLists.size(); i++){
					ballLists.get(i).draw(canvas);
					if(bExit) break;
				}
				mHolder.unlockCanvasAndPost(canvas);
			}//end of synchronized
			
			try {
				Thread.sleep(SurfView.DELAY);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}//endof while
	}// end of method run()
}//end of class DrawThread

	
}

